<template>
	<view class="container">
		<view v-if="planList.length > 0" class="plan-list">
			<h1 style="font-weight: 600; margin-bottom: 15px;">{{title}}</h1>
			<checkbox-group class="checkbox-group" @change="handleCheckboxChange">
				<div class="plan-items">
					<label v-for="(item, index) in planList" :key="index" class="plan-item">
						<checkbox class="checkbox" color="#FFCC33" style="transform:scale(0.7)" :value="index"
							:checked="selectedItems.includes(index)" />
						<span :class="{ strikethrough: selectedItems.includes(index) }" style="flex-grow: 1;">
							{{ item }}
						</span>
						<view @click.stop="openEditPopup(index)">
							🎫
						</view>
					</label>
				</div>
			</checkbox-group>
		</view>
		<view v-else class="input-box">
			<span>{{tip}}</span>
			<view class="ask">
				<input v-model="input" class="input" type="text" @confirm="addNewItem" />
				<view class="btn" @click="addNewItem">
					按钮
				</view>
			</view>
		</view>
		<!-- 弹出层 -->
		<view style="border-radius: 15px;">
			<!-- 普通弹窗 -->
			<uni-popup ref="popup" background-color="#fff" @change="change">
				<view class="popup-content">
					<view class="pop-btn" @click="openPopupForInput('insert')">插入</view>
					<view class="pop-btn" @click="openPopupForInput('edit')">编辑</view>
					<view class="pop-btn" @click="removeItem">删除</view>
					<view class="pop-btn" @click="closePopup">取消</view>
				</view>
			</uni-popup>
			<!-- 关闭\编辑弹窗 -->
			<uni-popup ref="closeEditPopup" background-color="#fff" @change="change">
				<view class="popup-content">
					<input v-model="popupInput" class="input" type="text" placeholder="请输入内容" />
					<view class="edit-btn">
						<view class="pop-btn-2" @click="saveItem">保存</view>
						<view class="pop-btn-2" @click="closePopup">取消</view>
					</view>

				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		singleTalk
	} from '@/api/talk/talk.js'

	export default {
		data() {
			return {
				planList: [
				],
				selectedItems: [],
				newItem: '',
				popupInput: '',
				title: '计划',
				tip: '你想做什么？',
				index: 0,
				think: false,
				type: '',
				input: ''
			}
		},
		onShow() {

		},
		methods: {
			getplan() {

				this.tip = '思考中...'

				if (this.think) {
					return
				}

				this.think = true;

				let message = [{
						role: 'system',
						content: '任务细分格式(空格分割,以此类推):步骤一 步骤二 步骤三 ...'
					},
					{
						role: 'user',
						content: `把大象放进冰箱门`
					},
					{
						role: "assistant",
						content: '打开冰箱门 获取大象 将大象移动到冰箱前 检查大象的大小是否适合冰箱 将大象放进冰箱 关闭冰箱门'
					},
					{
						role: 'user',
						content: `软考复习`
					},
					{
						role: "assistant",
						content: '准备复习环境 确定复习章节 设定复习时间 开始复习 复习结束'
					},
					{
						role: 'user',
						content: this.input
					}
				]
				singleTalk(message).then(res => {
					this.input = ''
					// console.log(res);
					this.planList = [];
					res.data.choices[0].message.content.split(' ').forEach(item => {
						this.planList.push(item)

					})
				})

			},
			addNewItem() {
				this.title = this.input
				this.getplan()
			},
			removeItem() {
				// Remove the selected item from the list
				this.planList.splice(this.index, 1);

				// Update the selectedItems array by removing the deleted item and adjusting remaining indices
				this.selectedItems = this.selectedItems
					.filter(item => item !== this.index) // Remove the deleted index if it was selected
					.map(item => (item > this.index ? item - 1 : item)); // Shift indices for items after the deleted one

				// Manually update the checkbox group to reflect the correct selected items
				this.$nextTick(() => {
					this.$refs.checkboxGroup.value = [...this.selectedItems];
				});

				// Close the popup after deletion
				this.$refs.popup.close();
			},

			openEditPopup(index) {
				this.index = index;
				this.popupInput = this.planList[index];
				this.$refs.popup.open('center');
			},
			handleCheckboxChange(event) {
				this.selectedItems = event.detail.value.map(Number);
			},
			openPopupForInput(type) {
				this.type = type;
				if (type === 'insert') {
					this.popupInput = '';
				}
				this.$refs.popup.close();
				this.$refs.closeEditPopup.open('center');
			},
			saveItem() {
				if (this.type === 'edit') {
					if (this.popupInput.trim() !== '') {
						this.$set(this.planList, this.index, this.popupInput.trim());
					}
				} else if (this.type === 'insert') {
					if (this.popupInput.trim() !== '') {
						this.planList.push(this.popupInput.trim());
					}
				}
				this.$refs.closeEditPopup.close();
			},
			closePopup() {
				this.popupInput = '';
				this.$refs.popup.close() || this.$refs.closeEditPopup.close();
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #212121;
		color: white;
		height: 100vh;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.input-box {
		display: flex;
		flex-direction: column;
		width: 100%;
		align-items: center;
	}

	.ask {
		margin-top: 20px;
		display: flex;
		border-radius: 25px;
		padding: 10px 20px;
		width: 70%;
		background-color: #2F2F2F;
		box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.2);
	}

	.input {
		flex-grow: 1;
	}

	.plan-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		overflow-y: auto;
		max-height: 500px;
		width: 100%;
	}

	.plan-items {
		display: flex;
		flex-direction: column;
		width: 100%;
		gap: 10px;
	}

	.checkbox-group {
		width: 80%;
	}

	.plan-item {
		background-color: #2F2F2F;
		border-radius: 5px;
		display: flex;
		align-items: baseline;
		padding: 10px 10px;
	}

	.strikethrough {
		text-decoration: line-through;
		color: #888;
	}

	.popup-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px;
		width: 300px;
		border-radius: 10px;
		background-color: #212121;
		min-height: 150px;
	}

	.pop-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		padding: 15px 0px;
		margin: 10px;
		border-radius: 10px;
		background-color: #2F2F2F;
	}

	.pop-btn-2 {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		padding: 15px 0px;
		margin: 5px;
		border-radius: 10px;
		background-color: #2F2F2F;
	}

	.edit-btn {
		display: flex;
		align-items: center;
		justify-content: space-around;
		width: 100%;
	}

	/deep/.uni-popup__wrapper {
		border-radius: 10px;
	}
</style>